//
//  ST_BTGiftModel.swift
//  btvideo
//
//  Created by mac on 2020/6/28.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import HandyJSON

class ST_BTGiftModel: NSObject {
    
    struct NobleWidget {
        let id:Int
        let name:String
        let canSend:Bool
    }
    
    var isSelected = false{
        didSet{
//            print("奇怪bug双选中")
        }
    }
    
    var tabType = GoodsType.gift
//    礼物使用
    var imageUrl = ""
    var title = ""
    var content = ""
    var durDayStr = ""
    
    var give_member:BTMemberGoodsModel?
    
    var giftLeftTopWidgetImageName:String?
    
    /// 贵族礼物
    var nobleWidget:NobleWidget?
    
    var svgaUrl:String?
//    会员使用
    var imageLocal = ""
    var purMonths = 0
    var currentPrice = 0
    var currentPriceMonthlyStr = ""
    
    var common:BTGoodsModel?
    
    /// 盲盒
    var isBlindBox:Bool = false
    /// 盲盒介绍
    var skipUrl:String?
    var skipTitle:String?
    
    init(json:JSON,giftType:GoodsType) {
        super.init()
        
        self.isBlindBox = json["is_blind_box"].boolValue
        
        self.skipUrl = json["skip_url"].string
        
        self.skipTitle = json["skip_title"].string
        
        self.tabType = giftType
        
        /// 贵族礼物
        if let nobilityId = json["nobility_id"].int{
            
            nobleWidget = NobleWidget(id: nobilityId, name: json["nobility_name"].stringValue, canSend: json["can_send"].boolValue)
            
            giftLeftTopWidgetImageName = "gift_noble_" + nobilityId.string
            
        }else if let typeInt = json["style_type"].int,typeInt > 0{
            
            giftLeftTopWidgetImageName = "giftType0" + (typeInt - 1).string
            
        }
        
        
        
        if let url = json["svga_url"].string,url.count > 0{
            svgaUrl = url
        }
        
        
        switch giftType {
            
        case .gift,.sweetGuard:
           
            let m = BTGoodsModel.init(fromJson: json)
            m.type = giftType
           common = m
           imageUrl = m.content
           title = m.name
           content =  m.useCoin.string + "个甜心币"
            if giftType == .sweetGuard{
                durDayStr = m.effectiveTime.string + "天"
            }else{
                durDayStr = ""
            }
           
            
        default:
            if let model = BTMemberGoodsModel.deserialize(from: json.rawString()){
                
                give_member = model
                
                imageLocal = "会员_icon_超级大"
                
                purMonths = model.limited_time
                
                currentPrice = model.original_price.int
                
                
                if model.limited_time > 1{
                    
                    let f = model.original_price.float/model.limited_time.float
                    
                    let s = "¥"+String.init(format: "%.2f", f)+"/月"
                    
                    currentPriceMonthlyStr = s
                }else{
                    currentPriceMonthlyStr = ""
                }
            }
        }
    }
    
}


class BTGiftTabModel: NSObject {
    
    var title = ""
    
    var array = [ST_BTGiftModel]()
    
    
    lazy var tabPages:Int = {
        
        let n = (self.array.count.float/(ST_GiftItemView.rowCount.float*ST_GiftItemView.columnCount.float)).ceil.int
        
        return n
    }()
    
}





//MARK: - Model
/**
 会员商品
 */
class BTMemberGoodsModel: NSObject,HandyJSON {
    
    var id = 0
    /** 打折之后支付价格
     */
    var original_price = 0.0
    /**
     打折之前的价格
     */
    var limited_price = 0.0
    /**
     12 月  6 月 1月
     */
    var limited_time = 1
    //产品id
    var ename = ""
    
    var descr = ""
    
    var alternative_descr = ""
    
    var detail_desc = ""
    /**
     送会员提升亲密度
     */
    var intimacy = 0
    
    /**
     附加文案
     */
    var thrift = ""
    
    /**
     后台返回颜色
     */
    var color = ""
    
    /**
     是否显示边框  == 1展示边框
     */
    var border = 0
    
    required override init() {
        super.init()
    }
    
}

enum GoodsType {
    /** 礼物
     */
    case gift
    /**守护
     */
    case sweetGuard
    /**会员
     */
    case member
}

/**
 内部商品
 */
class BTGoodsModel : NSObject{

    var content : String!
    var effectiveTime : Int!
    var id : Int!
    var intimacy : Int!
    var name : String!
    var useCoin : Int!
    var type:GoodsType!
    
    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON!){
        if json.isEmpty{
            return
        }
        content = json["content"].stringValue
        effectiveTime = json["effective_time"].intValue
        id = json["id"].intValue
        name = json["name"].stringValue
        useCoin = json["use_coin"].intValue
        intimacy = json["intimacy"].intValue
        
        if intimacy > 0{
            type = .sweetGuard
        }else{
            type = .gift
        }
        
    }
}
